using System;
using NFreeMarker.Ext.Util;
using NFreeMarker.Template;

namespace NFreeMarker.Ext.Beans
{
    /**
     * Wraps arbitrary subclass of {@link java.util.Date} into a reflective model.
     * Beside acting as a {@link TemplateDateModel}, you can call all Java methods
     * on these objects as well.
     */
    public class DateModel : BeanModel, ITemplateDateModel
    {
        internal static readonly IModelFactory Factory = new ModelFactoryImpl();

        private readonly int _type;
        
        /**
         * Creates a new model that wraps the specified date object.
         * @param date the date object to wrap into a model.
         * @param wrapper the {@link BeansWrapper} associated with this model.
         * Every model has to have an associated {@link BeansWrapper} instance. The
         * model gains many attributes from its wrapper, including the caching 
         * behavior, method exposure level, method-over-item shadowing policy etc.
         */
        public DateModel(DateTimeOffset date, BeansWrapper wrapper)
            : base(date, wrapper)
        {
            _type = wrapper.GetDefaultDateType();
        }

        public DateTimeOffset GetAsDate()
        {
            return (DateTimeOffset) Object;
        }

        public int GetDateType()
        {
            return _type;
        }

        private class ModelFactoryImpl : IModelFactory
        {
            public ITemplateModel Create(object obj, IObjectWrapper wrapper)
            {
                return new DateModel((DateTimeOffset) obj, (BeansWrapper) wrapper);
            }
        }
    }
}